package entite;

import ia.Comportement;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Map;

public class EntityProperties {

	public final static String ENTITES_PROPERTY = "entite";
	private final static String ENTITES_SEPARATOR = ";";
	private final static String ENTITE_IMAGE_PROPERTY = "-image";
	private final static String ENTITE_COMPORTEMENT_PROPERTY = "-comportement";
	private final static String ENTITE_ATTAQUE_PROPERTY = "-att";
	private final static String ENTITE_DEFENSE_PROPERTY = "-def";
	private final static String ENTITE_PORTEE_PROPERTY = "-por";
	private final static Map<String, String> s_properties = new Hashtable<String, String>();
	
	private EntityProperties(){
	}
	
	public static Map<String, String> getInstance() {
		return s_properties;
	}
	
	public static void load(String propertiesFilename)			
		throws FileNotFoundException, IOException, URISyntaxException {
		URL url = EntityProperties.class.getResource(propertiesFilename);
		FileInputStream fis = new FileInputStream(new File(url.toURI()));
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		String ligne;
		while ((ligne=br.readLine())!=null){
			int indexOf = ligne.indexOf("=");
			if(indexOf != -1){
				s_properties.put(ligne.substring(0, indexOf), ligne.substring(indexOf + 1));
				System.out.println(ligne.substring(0, indexOf) + " # " + ligne.substring(indexOf + 1));
			}
		}
		br.close();
	}
	
	public static Caracteristique getCarac(String key) {
		Caracteristique carac = new Caracteristique();
		carac.armure = getInt(key + ENTITE_DEFENSE_PROPERTY);
		carac.degats = getInt(key + ENTITE_ATTAQUE_PROPERTY);
		int porteeDirect = getInt(key + ENTITE_PORTEE_PROPERTY);
		System.out.println("#### " + porteeDirect);
		// TODO : a revoir
		carac.portee = (float) Math.sqrt(porteeDirect * porteeDirect*2);
		carac.vie = carac.armure;
		System.out.println("#Key : " + key);
		System.out.println("#armure : " + carac.armure);
		System.out.println("#degats : " + carac.degats);
		System.out.println("#portee : " + carac.portee);
		System.out.println("#vie : " + carac.vie);
		return carac;
	}
	
	private static int getInt(String entiteProperty) {
		int result = - 1;
		try{
			result = Integer.parseInt(s_properties.get(entiteProperty));
		} catch(Exception e) {
			
		}
		return result;
	}
	
	public static String[] getTypes() {
		return s_properties.get(ENTITES_PROPERTY).split(ENTITES_SEPARATOR);
	}
	
	public static String getImage(String key) {
		return s_properties.get(key + ENTITE_IMAGE_PROPERTY);
	}
	
	public static Comportement getComportement(String key) {
		Comportement comportement = new Comportement();
		comportement.agressivite = Comportement.AGRESSIVITE.valueOf(s_properties.get(key + ENTITE_COMPORTEMENT_PROPERTY));
		return comportement;
	}
}
